import {Component} from '@angular/core';
import {IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {CustomerProvider} from "@providers/interface/customer";
import {StorageProvider} from "@providers/storage";
import {DialogProvider} from "@providers/dialog";

/**
 * Generated class for the CustomerAddressListPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-customer-address-list',
  templateUrl: 'customer-address-list.html',
})
export class CustomerAddressListPage {

  addlist: any[] = [];

  userinfo: any = this.storage.read("userInfo");

  constructor(public navCtrl: NavController,
              private viewCtrl: ViewController,
              public navParams: NavParams,
              private customerProvider: CustomerProvider,
              private storage: StorageProvider,
              private dialog: DialogProvider,) {
  }

  ionViewWillEnter() {
    console.log('ionViewDidLoad CustomerAddressListPage');
    this.customerProvider
      .getCustomerInfo({"PARTYID": this.userinfo.PARTYID})
      .then((message) => {
        this.addlist = message.data
      })
  }

  push(page: string, data?: any) {
    this.navCtrl.push(page, data);
  }

  choose(ad: any) {
    this.addlist.forEach((v) => {
      if (v.ID != ad.ID) {
        v.STUATS = "0";
      }
    });
    this.customerProvider
      .chooseCustomerDefortAddress(this.userinfo.PARTYID, ad.ID)
      .then(() => {
        ad.STUATS = "1";
      }).catch((error) => {
      this.dialog.showAlert('设置失败！'+error);
    });
  }

  edit(ad: any, page: string) {
    ad.title='地址编辑';
    this.navCtrl.push(page, ad);
  }

  delete(ad: any, i: number) {
    this.dialog.showConfirm(() => {
      this.customerProvider
        .deleteCustomerAddress(this.userinfo.PARTYID, ad.id)
        .then(()=>{
          this.addlist.splice(i, 1)
        })
        .catch((error)=>{
          this.dialog.showAlert('删除失败！'+error);
        })
    }, "请确认是否删除该地址！");
  }

  dismiss(ad?: any) {
    this.viewCtrl.dismiss(ad);
  }

}
